package cn.cloud.mqtt2redis.service;

import cn.cloud.mqtt2redis.configuration.mysql.IdClass;
import cn.cloud.mqtt2redis.configuration.mysql.progMapEntity;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import cn.cloud.mqtt2redis.dao.mysqlDao;

import java.util.List;
import java.util.Optional;

@Service
public class MysqlService {
    private Logger logger = LoggerFactory.getLogger(getClass());
    @Autowired
    private mysqlDao mysqlDao;

    //按照主键查询一条记录
    //数据库中，以device和name为复合主键，按照device和name进行查询，结果唯一
    public progMapEntity query_by_deviceAndName(String device, String name) {
        Optional<progMapEntity> record= mysqlDao.findById(new IdClass(device,name));
        if(record.isPresent()) return record.get();
        return null;
    }

    //查询数据库所有记录
    public List<progMapEntity> query_all()
    {
        return mysqlDao.findAll();
    }

    //增加一条记录
    public void save_to_mysql(progMapEntity record) {
        mysqlDao.save(record);
    }
    public void save_to_mysql(String device, String name, String prog_id, String time, String path) {
        progMapEntity record = new progMapEntity(device,name,prog_id,time,path);
        mysqlDao.save(record);
    }

    //更新一条记录
    public void update_to_mysql(progMapEntity record) {
        mysqlDao.save(record);
    }
    public void update_to_mysql(String device, String name, String prog_id, String time, String path) {
        progMapEntity record = new progMapEntity(device,name,prog_id,time,path);
        mysqlDao.save(record);
    }
}
